/*
 * Decompiled with CFR 0.150.
 * 
 * Could not load the following classes:
 *  cn.hutool.core.util.ObjectUtil
 *  com.alibaba.fastjson.JSON
 *  de.felixroske.jfxsupport.FXMLController
 *  javafx.beans.value.ChangeListener
 *  javafx.beans.value.ObservableValue
 *  javafx.fxml.FXML
 *  javafx.fxml.Initializable
 *  javafx.scene.control.Alert
 *  javafx.scene.control.Alert$AlertType
 *  javafx.scene.control.TextField
 *  javafx.scene.layout.AnchorPane
 *  org.springframework.beans.factory.annotation.Autowired
 */
package com.gys.controller.pay;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.gys.common.business.PayUtil;
import com.gys.common.data.HttpJson;
import com.gys.common.util.CacheUtil;
import com.gys.common.util.ValidationUtil;
import com.gys.controller.BaseController;
import com.gys.controller.dataManger.ControllerManger;
import com.gys.controller.pay.PayController;
import com.gys.controller.salesReceipts.SalesReceiptsMainController;
import com.gys.data.example.GetLoginStoreOutData;
import com.gys.data.pay.ExchangeCashOutData;
import com.gys.data.pay.GetPayInData;
import de.felixroske.jfxsupport.FXMLController;
import java.math.BigDecimal;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Alert;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import org.springframework.beans.factory.annotation.Autowired;

@FXMLController
public class ExchangeCashController
extends BaseController
implements Initializable {
    @FXML
    private AnchorPane pane;
    @FXML
    private TextField phone;
    @FXML
    private TextField name;
    @FXML
    private TextField point;
    @FXML
    private TextField maxDkAmount;
    @FXML
    public TextField dkAmount;
    @FXML
    public TextField surplusPoint;
    @Autowired
    private PayController payController;
    @Autowired
    private SalesReceiptsMainController salesReceiptsMainController;
    public String rate;
    public String isDouble;
    public boolean flag = true;

    public void initialize(URL location, ResourceBundle resources) {
        ControllerManger.controllerMap.put(this.getClass().getSimpleName(), this);
        this.init();
    }

    @Override
    public void init() {
        if (ObjectUtil.isEmpty(this.pane)) {
            return;
        }
        this.phone.setText(this.salesReceiptsMainController.getPhone().getText());
        this.name.setText(this.salesReceiptsMainController.getName().getText());
        this.point.setText(this.salesReceiptsMainController.getPoint().getText());
        GetPayInData payInData = new GetPayInData();
        payInData.setStoreCode(((GetLoginStoreOutData)CacheUtil.get("store")).getStoCode());
        payInData.setCardType(this.salesReceiptsMainController.getTypeId());
        HttpJson httpJson = PayUtil.exchangeCash(payInData);
        ExchangeCashOutData data = new ExchangeCashOutData();
        if (httpJson.isSuccess()) {
            data = (ExchangeCashOutData)JSON.parseObject((String)httpJson.getMsg(), ExchangeCashOutData.class);
            this.maxDkAmount.setText(data.getMaxDkAmount().toString());
            this.rate = data.getRate();
            this.isDouble = data.getExchangeRepeat();
        } else {
            Alert alert = this.createAlert(Alert.AlertType.INFORMATION, "\u63d0\u793a\u4fe1\u606f", httpJson.getMsg());
            alert.showAndWait();
            this.pane.getScene().getWindow().hide();
        }
        this.dkAmount.textProperty().addListener(new ChangeListener(){

            public void changed(ObservableValue observable, Object oldValue, Object newValue) {
                ExchangeCashController.this.surplusPoint.setText(ExchangeCashController.this.point.getText());
                String value = ExchangeCashController.this.dkAmount.getText();
                if (ObjectUtil.isNotEmpty(value) && ValidationUtil.checkAmount(ExchangeCashController.this.dkAmount.getText())) {
                    BigDecimal dkPoint = new BigDecimal(value).multiply(new BigDecimal(ExchangeCashController.this.rate));
                    ExchangeCashController.this.surplusPoint.setText(new BigDecimal(ExchangeCashController.this.point.getText()).subtract(dkPoint).toString());
                }
            }
        });
    }

    @FXML
    private void sureAction() {
        if (ObjectUtil.isEmpty(this.dkAmount.getText())) {
            Alert alert = this.createAlert(Alert.AlertType.INFORMATION, "\u63d0\u793a\u4fe1\u606f", "\u62b5\u6263\u91d1\u989d\u8f93\u5165\u4e0d\u5b8c\u6574\uff01");
            alert.showAndWait();
            return;
        }
        if ("1".equals(this.isDouble)) {
            if (!ValidationUtil.checkAmount(this.dkAmount.getText())) {
                Alert alert = this.createAlert(Alert.AlertType.INFORMATION, "\u63d0\u793a\u4fe1\u606f", "\u62b5\u6263\u91d1\u989d\u8f93\u5165\u4e0d\u5408\u6cd5\uff01");
                alert.showAndWait();
                return;
            }
        } else if (!ValidationUtil.checkPositiveInteger(this.dkAmount.getText())) {
            Alert alert = this.createAlert(Alert.AlertType.INFORMATION, "\u63d0\u793a\u4fe1\u606f", "\u62b5\u6263\u91d1\u989d\u8f93\u5165\u4e0d\u5408\u6cd5\uff01");
            alert.showAndWait();
            return;
        }
        if (new BigDecimal(this.surplusPoint.getText()).compareTo(BigDecimal.ZERO) < 0) {
            Alert alert = this.createAlert(Alert.AlertType.INFORMATION, "\u63d0\u793a\u4fe1\u606f", "\u4f1a\u5458\u79ef\u5206\u4f59\u989d\u4e0d\u8db3\uff01");
            alert.showAndWait();
            return;
        }
        if (new BigDecimal(this.dkAmount.getText()).compareTo(new BigDecimal(this.maxDkAmount.getText())) > 0) {
            Alert alert = this.createAlert(Alert.AlertType.INFORMATION, "\u63d0\u793a\u4fe1\u606f", "\u62b5\u6263\u91d1\u989d\u5927\u4e8e\u53ef\u62b5\u6263\u6700\u5927\u91d1\u989d\uff01");
            alert.showAndWait();
            return;
        }
        if (new BigDecimal(this.dkAmount.getText()).compareTo(new BigDecimal(this.payController.getDsAmount().getText())) > 0) {
            Alert alert = this.createAlert(Alert.AlertType.INFORMATION, "\u63d0\u793a\u4fe1\u606f", "\u62b5\u6263\u91d1\u989d\u4e0d\u80fd\u5927\u4e8e\u5f85\u4ed8\u91d1\u989d");
            alert.showAndWait();
            return;
        }
        this.payController.refreshAmount("9001", this.dkAmount.getText());
        this.payController.setJfdkPoint(new BigDecimal(this.dkAmount.getText()).multiply(new BigDecimal(this.rate)).toString());
        this.payController.setJfdkAmount(this.dkAmount.getText());
        this.payController.setCardNo(this.salesReceiptsMainController.getCardNum().getText());
        this.pane.getScene().getWindow().hide();
    }

    @FXML
    private void cancelAction() {
        this.surplusPoint.setText("");
        this.dkAmount.setText("");
        this.pane.getScene().getWindow().hide();
    }
}

